var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var articleInfos = require('../models/articleInfos');
var bodyParser = require('body-parser');
var app = express();

/* GET users listing. */

mongoose.connect('mongodb://127.0.0.1:27017/myBlog');

mongoose.connection.on('connected',function () {
    console.log("MongoDB connected success.")
});
mongoose.connection.on('err',function () {
    console.log("MongoDB connected fail.")
});
mongoose.connection.on('disconnected',function () {
    console.log("MongoDB connected disconnected.")
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
router.get('/',function (req, res, next) {
    res.send(new Date())
})

router.post('/handleEditors',function(req, res, next){
    let article_name = req.body.headerName;
    let category = req.body.category;
    let author = req.body.author;
    let article_content = req.body.content;
    let abstract = req.body.abstract;
    let like_num = 0;
    let read_num = 0;
    let comment_num = 0;
    let time = req.body.time;
    let comment_name = '';
    let comment_time = '';
    let comment_content = '';
    let article_comment = [{
        comment_name,
        comment_time,
        comment_content
    }];
    let info = {
        article_name: article_name,
        category: category,
        time: time,
        author: author,
        read_num: read_num,
        like_num: like_num,
        comment_num: comment_num,
        article_content: article_content,
        article_comment: article_comment,
        abstract: abstract

    }
    articleInfos.create(info,(err, doc)=>{
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

router.get('/getArticleInfo',function (req, res, next) {
    articleInfos.find({},function (err, doc) {
        if(err) {
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

router.post('/getArticleInfoPagination',function (req, res, next) {
    let pageSize = req.body.pageNum - 1;
    articleInfos.find({},function (err, doc) {
        if(err) {
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    }).skip(pageSize * 5).limit(5);
});

router.post('/deleteArticle',function (req, res, next) {
    articleInfos.remove({_id: req.body.id},function (err,doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

router.post('/conditionSearch',function(req, res, next){
    let category = req.body.category;
    let time = req.body.time;
    if(time == ''){
        articleInfos.find({category: req.body.category},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
            return;
        });
    }else if(category == ''){
        articleInfos.find({time: req.body.time},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
            return;
        });
    }else{
        articleInfos.find({category: req.body.category,time: req.body.time},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
        });
    }
});

router.post('/getHandlePage',function(req, res, next){
    let pageNum = req.body.pageNum - 1;
    let category = req.body.category;
    let time = req.body.time;

    if(time == '' && category != ''){
        articleInfos.find({category: req.body.category},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
            return;
        }).skip(pageNum * 15).limit(15);
    }else if(category == '' && time != ''){
        articleInfos.find({time: req.body.time},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
            return;
        }).skip(pageNum * 15).limit(15);
    }else if(category != '' && time != '') {
        articleInfos.find({category: req.body.category,time: req.body.time},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
        }).skip(pageNum * 15).limit(15);
    }else{
        articleInfos.find({},function (err,doc) {
            if(err){
                res.json({
                    status: '1',
                    msg: err.message
                })
            }else{
                res.json({
                    status: '0',
                    msg: '',
                    result: {
                        count: doc.length,
                        list: doc
                    }
                })
            }
        }).skip(pageNum * 15).limit(15);
    }

});

router.post('/getCategoryArticle',function (req, res, next) {
    let category = req.body.category;
    articleInfos.find({category: category},function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});
router.post('/getCategoryPage',function (req, res, next) {
    let category = req.body.category;
    let pageNum = req.body.pageNum - 1;
    articleInfos.find({category: category},function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    }).skip(pageNum * 5).limit(5);
});
router.post('/getArticleDetail',function (req, res, next) {
    let article_id = req.body.article_id;
    articleInfos.find({_id: article_id},function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    });
});
router.post('/submitComment',function (req, res, next) {
    let content = req.body.comment_content;
    let man = req.body.comment_man;
    let time = req.body.comment_time;
    let article_id = req.body.id;
    articleInfos.update({_id: article_id},{
        $push: {
            article_comment:[{
                comment_name: man,
                comment_time:time,
                comment_content: content
            }]
        }
    },function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    });
});

router.post('/updateLikeNum',function (req, res, next) {
    let like_num = req.body.like_num;
    let article_id = req.body._id;
    articleInfos.update({_id: article_id},{
        $set: {
           like_num: like_num
        }
    },function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    });
});

router.post('/updateReadNum',function (req, res, next) {
    let read_num = req.body.read_num;
    let article_id = req.body._id;
    articleInfos.update({_id: article_id},{
        $set: {
            read_num: read_num
        }
    },function (err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    });
});

router.get('/getIndexTipsInfo',function (req, res, next) {
    articleInfos.find({}).sort({'read_num':-1}).limit(5).exec(function(err, doc) {
        if(err){
            res.json({
                status: '1',
                msg: err.message
            })
        }else{
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

module.exports = router;

